package com.engine.dfqlj.sync.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.addons.common.entity.ResultMsg;
import com.engine.addons.common.util.LogUtil;
import com.engine.addons.ecology.common.entity.RequestInfoBean;
import com.engine.addons.ecology.common.util.RequestInfoUtil;
import com.engine.addons.init.api.service.APIDataInitService;
import com.engine.addons.init.api.service.impl.APIDataInitServiceImpl;
import com.engine.addons.init.dataProcess.entity.DataFormTable;
import com.engine.addons.init.dataProcess.entity.Map4Json;
import com.engine.dfqlj.sync.util.SyncDataUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TransferDeviceAction implements Action {

    private final LogUtil LOG = new LogUtil(TransferDeviceAction.class);
    private String operationType;

    public String getOperationType() {
        return operationType;
    }

    public void setOperationType(String operationType) {
        this.operationType = operationType;
    }

    @Override
    public String execute(RequestInfo requestInfo) {

        try {
            LOG.writeActionIntoLog("operationType:"+operationType);
            LOG.writeActionIntoLog(requestInfo.getRequestid());

            RequestInfoBean rib = new RequestInfoBean(requestInfo);
            Map<String, Object> mainMap = rib.getMainMap();
            LOG.writeSystemLog("requestInfo:"+JSONObject.toJSONString(rib));
            List<Map4Json> map4JsonList = new ArrayList<>();
            map4JsonList.add(RequestInfoUtil.requestInfoToMap4Json(rib));

            String confName = "transfer";


            //处理数据
            APIDataInitService as = APIDataInitServiceImpl.getInstance();
            ResultMsg<DataFormTable> apiTableResultMsg = as.processData(null, map4JsonList, confName);

            if (!apiTableResultMsg.isStatus()) {
                requestInfo.getRequestManager().setMessagecontent("设备调拨出错：" + apiTableResultMsg.getText());
                return Action.FAILURE_AND_CONTINUE;
            }
            LOG.writeSystemLog("apiTableResultMsg:"+JSONObject.toJSONString(apiTableResultMsg));
            JSONObject sendJson = SyncDataUtil.covertTableToJson(apiTableResultMsg.getObject());
            {
                JSONArray res = new JSONArray();
                if(sendJson.containsKey("ZCFL") && sendJson.get("ZCFL").toString().equals("1")) {
                    return Action.SUCCESS;
                }
                res.add(sendJson);
                //调用接口
                ResultMsg<String> apiReturnMsg = SyncDataUtil.callSync(res.toJSONString(), "syncDevice");
                if (!apiReturnMsg.isStatus()) {
                    requestInfo.getRequestManager().setMessagecontent("调用设备调拨接口出错：" + apiReturnMsg.getText());
                    return Action.FAILURE_AND_CONTINUE;
                }
            }


//            if(!deviceLinesArray.isEmpty()) {
//                // 明细表循环调用
//                for (int i = 0; i < deviceLinesArray.size(); i++) {
//                    JSONObject detailJson = deviceLinesArray.getJSONObject(i);
//                    JSONObject mergeJsonObjects = SyncDataUtil.mergeJsonObjects(sendJson, detailJson);
//                    if(!mergeJsonObjects.isEmpty()) {
//                        mergeJsonObjects.remove("deviceLines");
//                    }
//                    //调用接口
//                    ResultMsg<String> apiReturnMsg = SyncDataUtil.callSync(mergeJsonObjects.toJSONString(), "syncDevice");
//
//                    if (!apiReturnMsg.isStatus()) {
//                        requestInfo.getRequestManager().setMessagecontent("调用设备调拨接口出错：" + apiReturnMsg.getText());
//                        return Action.FAILURE_AND_CONTINUE;
//                    }
//                }
//            }else{
//                //调用接口
//                ResultMsg<String> apiReturnMsg = SyncDataUtil.callSync(sendJson.toJSONString(), "syncDevice");
//                if (!apiReturnMsg.isStatus()) {
//                    requestInfo.getRequestManager().setMessagecontent("调用设备调拨接口出错：" + apiReturnMsg.getText());
//                    return Action.FAILURE_AND_CONTINUE;
//                }
//            }
            LOG.writeActionOverLog(requestInfo.getRequestid());
            return Action.SUCCESS;
        } catch (Exception e) {
            LOG.writeSystemLog("处理设备调拨接口出错" + e);
            requestInfo.getRequestManager().setMessagecontent("处理设备调拨接口出错" + e.getMessage());
        }

        return Action.FAILURE_AND_CONTINUE;
    }

}
